package com.atguigu.tingshu.handler;

import com.atguigu.tingshu.model.CDCEntity;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import top.javatool.canal.client.annotation.CanalTable;
import top.javatool.canal.client.handler.EntryHandler;

/**
 * @author atguigu-mqx
 * @ClassName AlbumInfoCdcHandler
 * @description: TODO
 * @date 2024年06月17日
 * @version: 1.0
 */
@Component
@Slf4j
@CanalTable("album_info")
public class AlbumInfoCdcHandler implements EntryHandler<CDCEntity> {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void insert(CDCEntity cdcEntity) {
        //  insert...
        //  EntryHandler.super.insert(cdcEntity);
    }

    @Override
    public void update(CDCEntity before, CDCEntity after) {
        log.info("after:\t监听到修改的数据{}",after.getId());
        log.info("before:\t监听到修改的数据{}",before.getId());
        //  主要任务就是删除数据：
        //  获取key=album:[1613]
        String key = "album:["+after.getId()+"]";
        redisTemplate.delete(key);
        //  EntryHandler.super.update(before, after);
    }

    @Override
    public void delete(CDCEntity cdcEntity) {
        String key = "album:["+cdcEntity.getId()+"]";
        redisTemplate.delete(key);
        //  EntryHandler.super.delete(cdcEntity);
    }
}
